﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
using 纸片人.win2dEffectControl;
using 纸片人.win2dEffectControl.DataClass;

namespace 纸片人.InterfaceFolder
{
    public abstract class ExportDataControl : Control, IExporter
    {
      
        public ExportDataControl()
        {
            this.Unloaded += ExportDataControl_Unloaded;
        }

        private void ExportDataControl_Unloaded(object sender, RoutedEventArgs e)
        {
            Destroy();
        }

        /// <summary>
        /// 获取或设置IsExporter的值
        /// </summary>  
        public bool IsExporter
        {
            get { return (bool)GetValue(IsExporterProperty); }
            set { SetValue(IsExporterProperty, value); }
        }

        /// <summary>
        /// 标识 IsExporter 依赖属性。
        /// </summary>
        public static readonly DependencyProperty IsExporterProperty =
            DependencyProperty.Register("IsExporter", typeof(bool), typeof(ExportDataControl), new PropertyMetadata(true, OnIsExporterChanged));

        private static void OnIsExporterChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
        {
            ExportDataControl target = obj as ExportDataControl;
            bool oldValue = (bool)args.OldValue;
            bool newValue = (bool)args.NewValue;
            if (oldValue != newValue)
                target.OnIsExporterChanged(oldValue, newValue);
        }

        protected virtual void OnIsExporterChanged(bool oldValue, bool newValue)
        {
        }

        /// <summary>
        /// 输出连线列表
        /// </summary>
        public List<ConnectLineUIElement> ListExportoneConnectElement  { get;set;}= new List<ConnectLineUIElement>();
        public OutputType Type { get; set; }

        /// <summary>
        /// 输出的控件拖拽事件发生了
        /// </summary>
        public event DragOutputEffectHeader DragOutputEffect;
        /// <summary>
        /// 断开所有连接
        /// </summary>
        public virtual event DeleteAllConnnectHeader DeleteAllConnnect;
        /// <summary>
        /// 发生了连接
        /// </summary>
        public event ConnectSourceHeader ConnectSource;
        /// <summary>
        /// 连接发生了更改
        /// </summary>
        public event ConnectSourceChangedHeader ConnectSourceChanged;

        public void AddExportoneConnectElement(ConnectLineUIElement newConnectLine)
        {
            ListExportoneConnectElement.Add(newConnectLine);
        }

        public void RemoveExportoneConnectElement(ConnectLineUIElement newConnectLine)
        {
            ListExportoneConnectElement.Remove(newConnectLine);
        }

        /// <summary>
        /// 移除指定的接受者
        /// </summary>
        /// <param name="connectElement"></param>
        /// <param name="data"></param>
        /// <exception cref="NotImplementedException"></exception>
        public abstract void RemoveProvider(ConnectLineUIElement connectElement);


        /// <summary>
        /// 删除控件
        /// </summary>
        /// <exception cref="NotImplementedException"></exception>
        public abstract void Destroy();


        /// <summary>
        /// 断开指定的输出连接，接收者删除指定的输入源，输出者删除指定的接收者
        /// </summary>
        /// <param name="targetControl"></param>
        public virtual  void Disconnect(ConnectLineUIElement connectElement)
        {
            //接收者删除指定的输入信息
            connectElement.Provider.RemoveExport(connectElement);
            //输出者删除指定的接收者
            RemoveProvider(connectElement);

        }


        /// <summary>
        /// 连接发生了更改
        /// </summary>
        /// <param name="newConnectLine"></param>
        /// <param name="oldConnectLine"></param>
        public void OnConnectSourceChangedInvoke(ConnectLineUIElement newConnectLine, ConnectLineUIElement oldConnectLine)
        {
            ConnectSourceChanged?.Invoke(newConnectLine, oldConnectLine);
        }

        /// <summary>
        /// 创建连接
        /// </summary>
        /// <param name="newConnectLine"></param>
        public void OnConnectSourceInvoke(ConnectLineUIElement newConnectLine)
        {
            ConnectSource?.Invoke(newConnectLine);
        }

        


        /// <summary>
        /// 获取模板里面指定类型，和指定名字的控件
        /// </summary>
        /// <typeparam name="T">指定的类型（泛型）</typeparam>
        /// <param name="name">指定的名字</param>
        /// <returns>如果找到该控件则返回，否则返回null</returns>
        protected T GetTemplateChildFormName<T>(string name = "") where T : DependencyObject
        {
            var element = GetTemplateChild(name);
            return (T)element;
        }

        /// <summary>
        /// 获取指定类型的子类，并放入传进来的列表中
        /// </summary>
        /// <typeparam name="T">指定的类型（泛型）</typeparam>
        /// <param name="name">指定的名字</param>
        /// <returns>如果找到该控件则返回，否则返回null</returns>
        protected void GetChildType<T>(DependencyObject reference,List<T> list) where T : DependencyObject
        {
            var count = VisualTreeHelper.GetChildrenCount(reference);
            for (int i = 0; i < count; i++)
            {
                var child = VisualTreeHelper.GetChild(reference,i);
                if(child is T)
                {
                    list.Add((T)child);
                }
                else
                {
                    GetChildType<T>(child,list);
                }
            }
        }

        /// <summary>
        /// 删除所有连接
        /// </summary>
        /// <param name="allConnectElement"></param>
        protected void DeleteAllConnnectInvoke(List<ConnectLineUIElement> allConnectElement)
        {
            DeleteAllConnnect?.Invoke(this, allConnectElement);

        }

        protected void OnDragExportInvoke()
        {
            DragOutputEffect?.Invoke(this);
        }


    }
}
